package com.hbyj.struts.action;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.hbyj.db.function.CDBInfo;
import com.hbyj.db.function.DbTools;
import com.hbyj.hbm.vo.YjczYjzy01;
import com.hbyj.hbm.vo.YjczYjzy02;
import com.hbyj.util.ChangeTool;
import com.hbyj.util.Constants;
import com.hbyj.util.PageClass;
import com.opensymphony.xwork2.ActionSupport;

public class YJDCSbglAction extends ActionSupport{
	private int total;
	private List rows = new ArrayList();
	private String jsonResult;
	public String getJsonResult() {
		return jsonResult;
	}
	public void setJsonResult(String jsonResult) {
		this.jsonResult = jsonResult;
	}
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	public List getRows() {
		return rows;
	}
	public void setRows(List rows) {
		this.rows = rows;
	}
	
	public String querySbglForSbgl(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String pageNo = request.getParameter("page");
		int pageSize = Constants.PAGE_KEY;
		String areaCode = request.getParameter("areaCode");
		String zylx = request.getParameter("zylx");
		String zyzt = request.getParameter("zyzt");//
		String conStr = "";
		if(!"".equals(zylx)){
			conStr += " and yjzy01030 ='"+zylx+"' ";
		}
		if(!"".equals(zyzt)){
			conStr += " and yjzy01060 ='"+zyzt+"'";
		}
		String hql = "from YjczYjzy01,Area where areaCode =yjzy01010 and yjzy01010 like '"+areaCode+"'"+conStr;
		String countHql = "select count(*) from YjczYjzy01,Area where areaCode =yjzy01010  and yjzy01010 like '"+areaCode+"'"+conStr;

		int t = DbTools.getDataHQLCount(countHql);
		PageClass page = new PageClass();
		page.setPageShow(pageSize);  //设置每页显示的行数
		page.setPageCount(t);  //设置总页数
		page.setCurrentPage(Integer.parseInt(pageNo));  //设置当前页
		int first=page.getNextIndex(); //获得开始的索引值
		int max=page.getPageShow();  //获得每次显示的值
		rows = (List)DbTools.findPageHQL(first, max,hql);
		total = t;
		return SUCCESS;
	}
	
	public void deleteSbForSbgl() throws UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		String sql = "delete from YjczYjzy01 where yjzy01000 = '"+id+"'";
		if(DbTools.delete(sql)){
			ServletActionContext.getResponse().getOutputStream().write("true".getBytes("utf-8"));
		}
	}
	
	public void updateSBForSbgl() throws UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String yjzy01Str = request.getParameter("yjzy01");
		JSONObject obj = JSONObject.fromObject(yjzy01Str);
		YjczYjzy01 yjzy01 = (YjczYjzy01)JSONObject.toBean(obj, YjczYjzy01.class);
		if(DbTools.updateObj(yjzy01)){
			ServletActionContext.getResponse().getOutputStream().write("true".getBytes("utf-8"));
		}
	}
	
	public void AddYjdcSBForSbgl() throws UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String yjzy01Str = request.getParameter("yjzy01");
		JSONObject obj = JSONObject.fromObject(yjzy01Str);
		YjczYjzy01 yjzy01 = (YjczYjzy01)JSONObject.toBean(obj, YjczYjzy01.class);
		if(DbTools.saveObj(yjzy01)){
			ServletActionContext.getResponse().getOutputStream().write("true".getBytes("utf-8"));
		}
	}
	
	public String querySbByState(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String pageNo = request.getParameter("page");
		String flag = request.getParameter("flag");
		int pageSize = Constants.PAGE_KEY;
		String areaCode = request.getParameter("areaCode");
		String zylx = request.getParameter("zylx");
		String conStr = "";
		if(!"".equals(zylx)){
			conStr += " and yjzy01030 ='"+zylx+"' ";
		}
		
		String hql = "from YjczYjzy01,Area where yjzy01060='1' and yjzy01090 ='"+flag+"' and areaCode =yjzy01010 and yjzy01010 like '"+areaCode+"'"+conStr;
		String countHql = "select count(*) from YjczYjzy01,Area where yjzy01060='1' and yjzy01090 ='"+flag+"' and areaCode =yjzy01010 and yjzy01010 like '"+areaCode+"'"+conStr;

		int t = DbTools.getDataHQLCount(countHql);
		PageClass page = new PageClass();
		page.setPageShow(pageSize);  //设置每页显示的行数
		page.setPageCount(t);  //设置总页数
		page.setCurrentPage(Integer.parseInt(pageNo));  //设置当前页
		int first=page.getNextIndex(); //获得开始的索引值
		int max=page.getPageShow();  //获得每次显示的值
		rows = (List)DbTools.findPageHQL(first, max,hql);
		total = t;
		return SUCCESS;
	}
	
	public void AddCkInfoForCkgl() throws UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String yjzy02Str = request.getParameter("yjzy02");
		JSONObject obj = JSONObject.fromObject(yjzy02Str);
		YjczYjzy02 yjzy02 = (YjczYjzy02)JSONObject.toBean(obj, YjczYjzy02.class);
		//拿到资源编号，去信息表里将yjzy01090改成“1”
		String yjzy01050 = yjzy02.getYjzy02050();
		if(DbTools.saveObj(yjzy02)&&ChangeYjzy01090ByYjzy01050(yjzy01050,"1")){
			ServletActionContext.getResponse().getOutputStream().write("true".getBytes("utf-8"));
		}else{
			ServletActionContext.getResponse().getOutputStream().write("false".getBytes("utf-8"));
		}
	}
	
	public boolean ChangeYjzy01090ByYjzy01050(String yjzy01050,String value){
		boolean flag = false;
		String hql = "from YjczYjzy01 where yjzy01050 ='"+yjzy01050+"'";
		List list = (List)DbTools.findByHql(hql);
		if(list.size()>0){
			YjczYjzy01 yjzy01 = (YjczYjzy01)list.get(0);
			yjzy01.setYjzy01090(value);
			if(DbTools.updateObj(yjzy01)){
				flag = true;
			}
		}
		return flag;
	}
	
	public void queryCkInfoForRkgl() throws UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String yjzy02050 = request.getParameter("yjzy02050");
		String hql = "from YjczYjzy02 where yjzy02050 = '"+yjzy02050+"' and yjzy02090 = null";
		List<YjczYjzy02> list = (List)DbTools.findByHql(hql);
		if(list.size()>0){
			JSONArray jsonArray = new JSONArray();
			for(int i = 0;i<list.size();i++){
				YjczYjzy02 yjcz02 = list.get(i);
				jsonArray.add(yjcz02);
			}
			ServletActionContext.getResponse().getOutputStream().write(jsonArray.toString().getBytes("utf-8"));
		}
	}
	
	public void AddRkInfoForRkgl() throws UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String yjzy02Str = request.getParameter("yjzy02");
		JSONObject obj = JSONObject.fromObject(yjzy02Str);
		YjczYjzy02 yjzy02 = (YjczYjzy02)JSONObject.toBean(obj, YjczYjzy02.class);
		//通过资源编号，将信息表之中的yjzy01050改为0
		//拿到资源编号，去信息表里将yjzy01090改成“1”
		String yjzy01050 = yjzy02.getYjzy02050();
		if(DbTools.updateObj(yjzy02)&&ChangeYjzy01090ByYjzy01050(yjzy01050,"0")){
			ServletActionContext.getResponse().getOutputStream().write("true".getBytes("utf-8"));
		}else{
			ServletActionContext.getResponse().getOutputStream().write("false".getBytes("utf-8"));
		}
	}
	/**
	 * author cuiyi
	 * time 2012-9-18
	 * disc 应急调查模块信息采集
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws IOException
	 */
	public String YJDCXXCJGetInfoFromServer() throws UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String tblStr = request.getParameter("tblList");//表名称字符串  tb1,tb2,tb3....
		String dbinfoStr = request.getParameter("dbinfo");//目标数据库信息
		String region = request.getParameter("region");
	//	region = "420000";
		JSONObject obj = JSONObject.fromObject(dbinfoStr);
		CDBInfo dbinfo = (CDBInfo)JSONObject.toBean(obj, CDBInfo.class);
		String[] tblList = tblStr.split(",");
		jsonResult = DbTools.getYJDCInfoFromServer(dbinfo, tblList,ChangeTool.ChangeArea(region));
		return SUCCESS;
	}
	
}
